package com.jing.lock;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public class NotSafeDemo {
    public static void main(String[] args) {
        //list();
        //set();
        Map map = new ConcurrentHashMap();//Collections.synchronizedMap(new HashMap<>());//new HashMap();
        for (int i = 0; i < 30; i++) {
            int finI = i;
            new Thread(() -> {
                String str = UUID.randomUUID().toString().substring(0, 8);
                map.put(finI,str);
                System.out.println(Thread.currentThread().getName() + "\t" + map);
            },String.valueOf(i)).start();
        }

    }

    private static void set() {
        Set set = new CopyOnWriteArraySet();//Collections.synchronizedSet(new HashSet<>());//new HashSet();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                String str = UUID.randomUUID().toString().substring(0, 8);
                set.add(str);
                System.out.println(Thread.currentThread().getName() + "\t" + set);
            },String.valueOf(i)).start();
        }
    }

    private static void list() {
        //List list = new ArrayList<>();
        //List list = new Vector();
        //List list = Collections.synchronizedList(new ArrayList<>());
        List list = new CopyOnWriteArrayList();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                String str = UUID.randomUUID().toString().substring(0, 8);
                list.add(str);
                System.out.println(list);
            },""+i).start();
        }
    }
}
